package remoteView;
import com.cloudgarden.resource.SWTResourceManager;

import java.util.ArrayList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import controller.CommandsExcuter;
import controller.CommandsExcuter.AddPlugExcutor;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class AddPluginBox extends org.eclipse.swt.widgets.Composite {

	{
		//Register as a resource user - SWTResourceManager will
		//handle the obtaining and disposing of resources
		SWTResourceManager.registerResourceUser(this);
	}
	
	private Label orderLabel;
	private Text textPath;
	private Button browseButton;
	private static Shell shell;
	private Label errLabel;
	private static Display display;
	private static ArrayList<String> tagList;
	private Button okButton;

	/**
	* Auto-generated main method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	*/
	public static void main(String[] args) {
		showGUI();
	}
	
	
	/**
	* Auto-generated method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	*/
	public static void showGUI() {
		display = Display.getDefault();
		shell = new Shell(display);
		AddPluginBox inst = new AddPluginBox(shell, SWT.NULL);
		Point size = inst.getSize();
		shell.setLayout(new FillLayout());
		shell.layout();
		if(size.x == 0 && size.y == 0) {
			inst.pack();
			shell.pack();
		} else {
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
			shell.setSize(shellBounds.width, shellBounds.height);
		}
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}
	/*
	 * constructor of the class
	 */
	public AddPluginBox(org.eclipse.swt.widgets.Composite parent, int style) {
		super(parent, style);
		initGUI();
	}
	/**
	 * initialize all GUI components
	 */
	private void initGUI() {
		try {
			FormLayout thisLayout = new FormLayout();
			this.setLayout(thisLayout);
			this.setSize(404, 143);
			{
				errLabel = new Label(this, SWT.NONE);
				FormData errLabelLData = new FormData();
				errLabelLData.left =  new FormAttachment(0, 1000, 14);
				errLabelLData.top =  new FormAttachment(0, 1000, 92);
				errLabelLData.width = 247;
				errLabelLData.height = 20;
				errLabel.setLayoutData(errLabelLData);
				errLabel.setText("Plugin already exist or wrong plugin!");
				errLabel.setForeground(SWTResourceManager.getColor(255, 0, 0));
				errLabel.setVisible(false);
			}
			{
				okButton = new Button(this, SWT.PUSH | SWT.CENTER);
				FormData okButtonLData = new FormData();
				okButtonLData.left =  new FormAttachment(0, 1000, 167);
				okButtonLData.top =  new FormAttachment(0, 1000, 111);
				okButtonLData.width = 49;
				okButtonLData.height = 24;
				okButton.setLayoutData(okButtonLData);
				okButton.setText("OK");
				okButton.setEnabled(false);
				okButton.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent evt) {
						okButtonWidgetSelected(evt);
					}
				});
			}
			{
				browseButton = new Button(this, SWT.PUSH | SWT.CENTER);
				FormData browseButtonLData = new FormData();
				browseButtonLData.left =  new FormAttachment(0, 1000, 324);
				browseButtonLData.top =  new FormAttachment(0, 1000, 57);
				browseButtonLData.width = 63;
				browseButtonLData.height = 27;
				browseButton.setLayoutData(browseButtonLData);
				browseButton.setText("Browse...");
				browseButton.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent evt) {
						browseButtonWidgetSelected(evt);
					}
				});
			}
			{
				FormData textPathLData = new FormData();
				textPathLData.left =  new FormAttachment(0, 1000, 12);
				textPathLData.top =  new FormAttachment(0, 1000, 57);
				textPathLData.width = 278;
				textPathLData.height = 24;
				textPath = new Text(this, SWT.NONE | SWT.BORDER);
				textPath.setLayoutData(textPathLData);
				textPath.setFocus();
				textPath.addKeyListener(new KeyListener() {
					
					@Override
					public void keyReleased(KeyEvent arg0) {
						if(textPath.getText().compareTo("")==0)
							okButton.setEnabled(false);
						else
							okButton.setEnabled(true);
					}
					
					@Override
					public void keyPressed(KeyEvent arg0) {}
				});
			}
			{
				orderLabel = new Label(this, SWT.NONE);
				FormData orderLabelLData = new FormData();
				orderLabelLData.left =  new FormAttachment(0, 1000, 12);
				orderLabelLData.top =  new FormAttachment(0, 1000, 30);
				orderLabelLData.width = 166;
				orderLabelLData.height = 21;
				orderLabel.setLayoutData(orderLabelLData);
				orderLabel.setText("Please specify plug-in to add:");
			}
			this.layout();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void browseButtonWidgetSelected(SelectionEvent evt) {
		errLabel.setVisible(false);
		FileDialog fd = new FileDialog(shell, SWT.OPEN);
        fd.setText("Open");
        String[] filterExt = { "*.class"};
        fd.setFilterExtensions(filterExt);
        String selected = fd.open();
        if(selected!=null){
        	textPath.setText(selected);
        	okButton.setEnabled(true);
        }
        

	}
	
	private void okButtonWidgetSelected(SelectionEvent evt) {
		//excute "add plug in" command
		AddPlugExcutor addPlugExcutor=CommandsExcuter.getCommand().getAddPlug();
		addPlugExcutor.setPath(textPath.getText());
		addPlugExcutor.excute();
		tagList=addPlugExcutor.getRetVal();
		if(tagList!=null)
			shell.dispose();
		else
			errLabel.setVisible(true);
	}


	public static ArrayList<String> getTagList() {
		return tagList;
	}
}
